package demo4.mapper;

import com.alibaba.druid.pool.DruidDataSource;
import demo4.config.DBUtil;
import demo4.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Component
public class UserMapper implements IUserMapper{
    @Autowired
    private DruidDataSource dataSource;
    @Override
    public int insert(User user) {
        try (Connection connection= dataSource.getConnection()){
            String sql="INSERT INTO user(username,password,nickname,age)\n" +
                    "        VALUES(?,?,?,?)";
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setString(1,user.getUsername());
            ps.setString(2,user.getPassword());
            ps.setString(3,user.getNickname());
            ps.setInt(4,user.getAge());
            return ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }
    @Override
    public User selectByUsername(String username) {
        try (Connection connection= dataSource.getConnection()){
            String sql="select id,username,nickname,age,password " +
                    "        from user where username=?";
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setString(1,username);
            ResultSet rs=ps.executeQuery();
            User user=new User();
            if(rs.next()){
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setNickname(rs.getString("nickname"));
                user.setAge(rs.getInt("age"));
                user.setId(rs.getInt("id"));
                return user;
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int login(String username, String password) {

        try (Connection connection= dataSource.getConnection()){
            String sql="select count(*) from user where username=? and password=?";
            PreparedStatement ps=
                    connection.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            ResultSet rs=ps.executeQuery();
            if(rs.next()){
                return rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }
    @Override
    public List<User> selectAll() {
        try (Connection connection= dataSource.getConnection()){
            String sql="select id,username,nickname,age,password " +
                    "        from user  ";
            PreparedStatement ps=connection.prepareStatement(sql);
            ResultSet rs=ps.executeQuery();
            List<User> list = new ArrayList<>();
            while (rs.next()){
                User user=new User();
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setNickname(rs.getString("nickname"));
                user.setAge(rs.getInt("age"));
                user.setId(rs.getInt("id"));
                list.add(user);
            }
            return list;
        }catch (SQLException throwables){
            throwables.printStackTrace();
        }
        return null;
    }



}
